<?php
class BaiduSpider {
	public static function analy($word, $index=null){
		$url = urlencode($word);
		$url = 'http://baike.baidu.com/search/word?word=' . $url . '&pic=1&sug=1&enc=utf8';
		$content = self::getHtml($url);
		$cs = split('<span class="headline-index">', $content);

		$h2s = null;
		$html = null;
// 		$html .= "<textarea style='width:100%;height:200px'>{$content}</textarea><br>";
		if(count($cs)>1){
			//有多条记录
			if($index === null){
				//如果还没有指定需要获取哪个
				$i = 0;
				foreach ($cs as $content){
					if($i === 0){
						++$i;
						continue;
					}
					$hs = split('</h2>', $content, 2);
					$content = $hs[0];
					$content = preg_replace('/<[^>]+>/im', '', $content);
					$h2s[] = $content;
					++$i;
				}
			}else{
				//如果已经指定获取哪个了
				$content = $cs[$index];
				$cs = split('</h1>', $content);
				$content = $cs[1];
				$indexs = self::getIndexs($content);
			}
		}else{
			$cs = split('<div class="card-summary-content">', $content);
			//$html .= "<textarea style='width:100%;height:200px'>{$content}</textarea><br>";
			if(count($cs) === 2){
				$content = $cs[1];
				$indexs = self::getIndexs ($content);
			}else{
				echo 1;exit;
				$indexs = array();
			}
		}
		
		
		
		return array(
			'h2s' => $h2s,
			'debug' => $html,
			'indexs' => $indexs
		);
	}
	/**
	 * @param content
	 * @param body
	 * @param h2
	 */private static function getIndexs($content) {
		//删除无用标签
		$content = preg_replace('%<(script|style|sup)[^>]*>.*?</\1>%sim', '', $content);
			
		//删除链接
		$content = preg_replace('%<\s*(a|span)[^>]*>(.*?)</\1>%', '$2', $content);
			
		//删除无关闭标签
		$content = preg_replace('%<(img)[^>]*/?>%', '', $content);
			
		//删除目录索引
		$content = preg_replace('%<div class="text_dir[^>]+>.*?</\s*div>%sim', '', $content);
			
		//删除无效底部内容
		$content = preg_replace('/^(.*)<div class="bk-album-collection-box log-set-param lazyload".*/sim', '$1', $content);
			
		//删除标签内属性
		$content = preg_replace('/<\s*(\w+) [^>]*>/sim', '<$1>', $content);
			
		//删除空标签
		$content = preg_replace('%<\s*(\w+)></\1>%sim', '', $content);
			
		$result = preg_split('/<h2>/sim', $content);
		$mingpian = $result[0];
		$mingpian = preg_replace('%</div>%sim', '', $mingpian);
		$mingpian = preg_replace('%<div>%sim', '', $mingpian);
		$mingpian = preg_replace('%\n|\t|\r%sim', '', $mingpian);
			
		$indexs = array('百科名片' => $mingpian);
		//处理其它目录
		for($i=1,$c=count($result);$i<$c;++$i){
			$body = preg_split('%</h2>%sim', $result[$i]);
			$h2 = $body[0];
			$h2 = preg_replace('%<\s*(span)[^>]*>(.*?)</\1>%', '$2', $h2);

			$body = $body[1];
			$body = preg_replace('/\n/sim', "<br>\n", $body);
			$indexs[$h2] = $body;
		}
		return $indexs;
	}

	
	public static function getHtml($url){
		$browser = array (
			"user_agent" => 'Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1',
			"language" => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
		);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			"User-Agent: {$browser['user_agent']}",
			"Accept-Language: {$browser['language']}"
		));
		
		$output = curl_exec($ch);
		
		$url = preg_replace('/.*Location:\s*([^\r|\n]+).*/s', '$1', $output);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_URL, 'http://baike.baidu.com' . $url);
		$output = curl_exec($ch);
		curl_close($ch);
		if ($output === FALSE) {
			return false;
		}
		//转换编码
		$output = iconv('gb2312', 'utf-8//IGNORE', $output);
		return $output;
	}
}

?>